package com.boot.mall.ware.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.common.constant.WareConstant;
import com.boot.mall.ware.entity.PurchaseDetailEntity;
import com.boot.mall.ware.service.PurchaseDetailService;
import com.boot.mall.ware.vo.PurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.common.utils.PageUtils;
import com.boot.common.utils.Query;

import com.boot.mall.ware.dao.PurchaseDao;
import com.boot.mall.ware.entity.PurchaseEntity;
import com.boot.mall.ware.service.PurchaseService;
import org.springframework.util.StringUtils;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils pagePurchase(Map<String, Object> params) {
        long current = Integer.parseInt(params.get("page").toString());
        long limit = Integer.parseInt(params.get("limit").toString());
        String key = (String) params.get("key");
        QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>();
//        if (!StringUtils.isEmpty(key)){
//            queryWrapper.eq("id",key)
//                    .or().like("name",key)
//                    .or().like("address",key)
//                    .or().like("areacode",key);
//        }
        Page<PurchaseEntity> page = new Page<>(current,limit);
        this.page(page,queryWrapper);
        // 结果封装
        PageUtils pageUtils = new PageUtils(page.getRecords(),(int)page.getTotal(),(int)page.getSize(),(int)current);
        return pageUtils;
    }

    @Override
    public PageUtils pagePurchaseUnReceive(Map<String, Object> params) {
        Page<PurchaseEntity> page = new Page<>();
        this.page(page,new QueryWrapper<PurchaseEntity>().eq("status","0").or().eq("status","1"));
        // 结果封装
        PageUtils pageUtils = new PageUtils(page.getRecords(),(int)page.getTotal(),(int)page.getSize(),1);
        return pageUtils;
    }

    @Override
    public void savePurchase(PurchaseVo purchaseVo) {
        Long purchaseId = purchaseVo.getPurchaseId();

        if (purchaseId == null){
            // 如果采购单的id为null，则新建一个采购单
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusCode.CREATED.getCode());
            baseMapper.insert(purchaseEntity);
            // 获取创建的采购单id
            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = purchaseVo.getItems();
        // 收集
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(item);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusCode.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        // 更新采购单的更新时间
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }
}